<?php

namespace App\Observers;

use App\Handlers\SlugTranslateHandler;
use App\Jobs\TranslateSlug;
use App\Models\Topic;

// creating, created, updating, updated, saving,
// saved,  deleting, deleted, restoring, restored

class TopicObserver
{
    public function creating(Topic $topic)
    {
        //
    }

    public function updating(Topic $topic)
    {
        //
    }

    public function saving(Topic $topic)
    {
        $topic ->body = clean($topic -> body,'user_topic_body');
        //xss过滤
        $topic->excerpt = make_excerpt($topic->body);
        //生成话题摘录

    }

    public function saved(Topic $topic)
    {
        if(!$topic->slug){
            dispatch(new TranslateSlug($topic));
            // 推送任务到队列
        }//如 slug 字段无内容，即使用翻译器对 title 进行翻译
    }
}